package au.com.helloweb.androidgamepractice.planegame2;

import java.util.Random;
import java.util.Vector;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import au.com.helloweb.androidgamepractice.MyApp;
import au.com.helloweb.androidgamepractice.R;
import au.com.helloweb.androidgamepractice.planegame.Boom;
import au.com.helloweb.androidgamepractice.planegame2.Enemy;
import au.com.helloweb.androidgamepractice.planegame2.Bullet;

public class MySurfaceView extends SurfaceView implements Callback, Runnable {
	public final static int GAME_MENU = 0; // game menu
	public final static int GAMING = 1;// in gaming
	public final static int GAME_WIN = 2;// game win
	public final static int GAME_LOST = 3;// game lost
	public final static int GAME_PAUSE = 4;// game pause
	public static int gameState = GAME_MENU;// set init default game state
	public static int screenW, screenH;
	private GameMenu gameMenu;
	private GameBg gameBg;
	private Player player;
	private Resources res = this.getResources();// declare a handler for load
												// image or resource
	private Bitmap bmpBackground;// game background
	private Bitmap bmpMenu;
	private Bitmap bmpBoom;
	private Bitmap bmpButton;
	private Bitmap bmpButtonPress;
	private Bitmap bmpPlayer;
	private Bitmap bmpPlayerBlood;
	
	private Bitmap bmpEnemyDuck;
	private Bitmap bmpEnemyBullet;
	private Bitmap bmpEnemyBoss;	
	
	
	private Bitmap bmpBullet;
	private Bitmap bmpEnemyFly;
	private Bitmap bmpBossBullet;
	
	private int count;//figure out how many times, so that add new enemy.
	private int createEnemyTimeGap = 50;
	private int countEnemyBullet = 0;
	private Vector<Enemy> vcEnemy;
	private Vector<Boom> vcBoom;
	private Vector<Bullet> vcBullet;
	private Vector<Bullet> vcBulletPlayer;
	private int countEnemy = 0;
	private int countBullet = 0;
	private int countPlayerBullet = 0;
	private boolean isBoss = false;
	private Random random;
	//private Vector<Boom> vcBoom;
	
	private Paint paint;
	private Canvas canvas;
	private SurfaceHolder sfh;
	private boolean flag;
	private Thread th;
	private Context ctx;
	private MyApp myApp = null;
	/**
	 * SurfaceViewʼ
	 */
	public MySurfaceView(Context context) {
		super(context);
		ctx = context;
		sfh = this.getHolder();
		sfh.addCallback(this);
		paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setAntiAlias(true);
		setFocusable(true);
		setFocusableInTouchMode(true);
		this.setKeepScreenOn(true);
		myApp = (MyApp)ctx.getApplicationContext();

	}
	/**
	 * SurfaceViewͼӦ˺
	 */
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		screenW = this.getWidth();
		screenH = this.getHeight();
		initGame();
		flag = true;
		th = new Thread(this);
		// ߳
		th.start();
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		flag = false;
	}

	@Override
	public void run() {
		while (flag) {
			long start = System.currentTimeMillis();
			myDraw();
			logic();
			long end = System.currentTimeMillis();
			try {
				if (end - start < 50) {
					Thread.sleep(50 - (end - start));
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	private void initGame() {
		Log.w("Test", "gameState:" + gameState);
		// enter game, then reset game menu
		if (gameState == GAME_MENU) {
			// load all game resource
			bmpBackground = BitmapFactory.decodeResource(res,
					R.drawable.background);
			bmpMenu = BitmapFactory.decodeResource(res, R.drawable.menu);
			bmpBoom = BitmapFactory.decodeResource(res,R.drawable.boom);
			bmpButton = BitmapFactory.decodeResource(res, R.drawable.button);
			bmpButtonPress = BitmapFactory.decodeResource(res,
					R.drawable.button);
			bmpPlayer = BitmapFactory.decodeResource(res, R.drawable.player);
			bmpPlayerBlood = BitmapFactory.decodeResource(res, R.drawable.hp);
			
			bmpBullet = BitmapFactory.decodeResource(res, R.drawable.bullet);
			bmpEnemyBullet = BitmapFactory.decodeResource(res, R.drawable.bullet_enemy);
			bmpBossBullet = BitmapFactory.decodeResource(res, R.drawable.boosbullet);
			
			bmpEnemyDuck = BitmapFactory.decodeResource(res, R.drawable.enemy_duck);
			bmpEnemyFly = BitmapFactory.decodeResource(res, R.drawable.enemy_fly);
			bmpEnemyBoss = BitmapFactory.decodeResource(res, R.drawable.enemy_pig);
		}
		gameMenu = new GameMenu(bmpMenu, bmpButton, bmpButtonPress);
		gameBg = new GameBg(bmpBackground);
		player = new Player(bmpPlayer,bmpPlayerBlood);
		
		vcEnemy = new Vector<Enemy>();
		vcBullet = new Vector<Bullet>();
		vcBulletPlayer = new Vector<Bullet>();
		
		random = new Random();
	}

	public void myDraw() {
		try {
			canvas = sfh.lockCanvas();
			// draw image by different status
			if (canvas != null) {
				canvas.drawColor(Color.WHITE);
				switch (gameState) {
				case GAME_MENU:
					gameMenu.draw(canvas, paint);
					break;
				case GAMING:
					gameBg.draw(canvas, paint);
					player.draw(canvas, paint);
					if(!isBoss){
						for(int i=0,j=vcEnemy.size();i<j;i++){
							vcEnemy.elementAt(i).draw(canvas, paint);
						}
						for(int i=0,j=vcBullet.size();i<j;i++){
							vcBullet.elementAt(i).draw(canvas, paint);
						}
						for(int i=0,j=vcBulletPlayer.size();i<j;i++){
							vcBulletPlayer.elementAt(i).draw(canvas, paint);
						}
					}else{
						
					}
					break;
				case GAME_WIN:
					break;
				case GAME_LOST:
					break;
				case GAME_PAUSE:
					break;
				}
			}
		} catch (Exception e) {

		} finally {
			if (canvas != null) {
				sfh.unlockCanvasAndPost(canvas);
			}
		}

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (gameState) {
		case GAME_MENU:
			gameMenu.onTouchEvent(event);
			break;
		case GAMING:
			
			break;
		case GAME_WIN:
			break;
		case GAME_LOST:
			break;
		case GAME_PAUSE:
			break;
		}
		return true;
	}

	private void logic() {
		switch (gameState) {
		case GAME_MENU:
			break;
		case GAMING:
			gameBg.logic();
			player.logic(myApp.getStatus());
			Log.w("game","GAMING:"+GAMING);
			if(!isBoss){
				//check enemy status
				for(int i=0;i<vcEnemy.size();i++){
					Enemy en = vcEnemy.elementAt(i);
					if(en.isDead){
						vcEnemy.removeElementAt(i);
					}else{
						en.logic();
					}
				}
				count++;
				Log.w("game","count:"+count);
				//after count to 50 mill seconds, start create enemy
				if(count % createEnemyTimeGap == 0){
					//get random number to add enemy
					int val = random.nextInt(4)%10;
					Log.w("game","val:"+val);
					int x = 0,y=0;
					switch(val){
					case 0:
					case 1://fly
						 x = random.nextInt(screenW-100)+50;
						vcEnemy.addElement(new Enemy(bmpEnemyFly,1,x,-50));
						break;
					case 2://duckL
//						 x = random.nextInt(screenH-100)+50;
//						vcEnemy.addElement(new Enemy(bmpEnemyDuck,2,-50,x));
						 y = random.nextInt(20);
						vcEnemy.addElement(new Enemy(bmpEnemyDuck, 2, -50, y));
						break;
					case 3://duckR
						 y = random.nextInt(20);
						vcEnemy.addElement(new Enemy(bmpEnemyDuck, 3, screenW + 50, y));
						break;
					}
				}
				
				//check collision with enemy or not
//				for(int i=0;i<vcEnemy.size();i++){
//					if(player.isCollsionWith(vcEnemy.elementAt(i))){
//						player.setPlayerBlood(player.getPlayerBlood()-1);
//						if(player.getPlayerBlood() <=-1){
//							gameState = GAME_LOST;
//						}
//					}
//				}
//				//add bullet
				countEnemyBullet++;
				if (countEnemyBullet % 40 == 0) {
				for(int i=0;i<vcEnemy.size();i++){
					Enemy en = vcEnemy.elementAt(i);
					switch(en.type){
					case 1://fly
						vcBullet.add(new Bullet(bmpEnemyBullet,en.x+10,en.y+20,Bullet.BULLET_FLY));
						break;
					case 2://duckL
						vcBullet.add(new Bullet(bmpEnemyBullet,en.x+10,en.y+20,Bullet.BULLET_DUCK));
						break;
					case 3://duckR
						vcBullet.add(new Bullet(bmpEnemyBullet,en.x+10,en.y+20,Bullet.BULLET_DUCK));
						break;
					}
				}
				}
//				//run bullet or remove it
				for(int i=0;i<vcBullet.size();i++){
					Bullet bt = vcBullet.elementAt(i);
					if(bt.isDead){
						vcBullet.removeElementAt(i);
					}else{
						bt.logic();
					}
				}
//				//collision with enemy's bullet
//				for(int i=0;i<vcBullet.size();i++){
//					if(player.isCollsionWith(vcBullet.elementAt(i))){
//						player.setPlayerBlood(player.getPlayerBlood()-1);
//						if(player.getPlayerBlood() <=-1){
//							gameState = GAME_LOST;
//						}
//					}
//				}
				
				for (int i = 0; i < vcBulletPlayer.size(); i++) {
					//ȡӵÿԪ
					Bullet blPlayer = vcBulletPlayer.elementAt(i);
					for (int j = 0; j < vcEnemy.size(); j++) {
						//ӱըЧ
						//ȡлÿԪӵж
						if (vcEnemy.elementAt(j).isCollsionWith(blPlayer)) {
							vcBoom.add(new Boom(bmpBoom, vcEnemy.elementAt(j).x, vcEnemy.elementAt(j).y, 7));
						}
					}
				}
			}else{
				
			}
			countPlayerBullet++;
			Log.w("game","countPlayerBullet:"+countPlayerBullet);
			if (countPlayerBullet % 20 == 0) {
				vcBulletPlayer.add(new Bullet(bmpBullet, player.x + 15, player.y - 20, Bullet.BULLET_PLAYER));
			}
			for (int i = 0; i < vcBulletPlayer.size(); i++) {
				Bullet b = vcBulletPlayer.elementAt(i);
				if (b.isDead) {
					vcBulletPlayer.removeElement(b);
				} else {
					b.logic();
				}
			}
			break;
		case GAME_WIN:
			break;
		case GAME_LOST:
			break;
		case GAME_PAUSE:
			break;
		}
	}
}
